package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.Check;
import cn.sxt.clinic.pojo.Dictionary;
import cn.sxt.clinic.pojo.ResultCode;
import cn.sxt.clinic.service.ICheckService;
import cn.sxt.clinic.service.IDictionaryService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/check")
public class CheckController {

    @Autowired
    private ICheckService checkService;

    @Autowired
    private IDictionaryService dictionaryService;

    @RequiresPermissions("check:checkManager")
    @RequestMapping("/checkManager")
    public String checkManager(){
        return "checkPojectCostManager";
    }

    @RequiresPermissions("check:list")
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   String keyword){
        IPage<Check> page=new Page<>(pageNum,pageSize);
        QueryWrapper<Check> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)){
            wrapper.like("check_name",keyword);
        }
        IPage<Check> result = checkService.page(page, wrapper);
        for (Check check : result.getRecords()) {
            Long checkDictionaryId = check.getCheckDictionaryId();
            Dictionary dictionary = dictionaryService.getById(checkDictionaryId);
            check.setDictionary(dictionary);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",result.getTotal());
        map.put("rows",result.getRecords());
        return map;
    }

    @RequiresPermissions("check:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public ResultCode delete(Integer checkId){
        boolean b = checkService.removeById(checkId);
        ResultCode resultCode;
        if (!b){
           resultCode= new ResultCode(0,"删除失败");
        }
        resultCode=new ResultCode(1,"删除成功");
        return  resultCode;
    }

    @RequestMapping("/checkEdit")
    public String checkEdit(Integer checkId, Model model){
//        System.out.println(checkId);
        if (checkId!=null){
            Check check = checkService.getById(checkId);
            model.addAttribute("check",check);
        }
        QueryWrapper<Dictionary> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id","1");
        List<Dictionary> list = dictionaryService.list(wrapper);
        model.addAttribute("dictionarys",list);
        return "checkPojectCostEdit";
    }

    @RequiresPermissions("check:insert")
    @RequestMapping("/insert")
    @ResponseBody
    public ResultCode insert(Check check){
        boolean save = checkService.save(check);
        ResultCode resultCode=null;
        if (save){
            resultCode=new ResultCode(1,"添加成功");
        }else{
            resultCode=new ResultCode(0,"添加失败");
        }
        return resultCode;
    }


    @RequiresPermissions("check:update")
    @RequestMapping("/update")
    @ResponseBody
    public ResultCode update(Check check){
        UpdateWrapper<Check> wrapper=new UpdateWrapper<>();
        wrapper.eq("check_id",check.getCheckId());
        boolean update = checkService.update(check, wrapper);
        ResultCode resultCode=null;
        if (update){
            resultCode=new ResultCode(1,"修改成功");
        }else{
            resultCode=new ResultCode(0,"修改失败");
        }
        return resultCode;
    }

}

